package rexsee.core.alarm;

import java.io.File;
import java.util.HashMap;

import rexsee.core.notification.NotificationArgumentsSheet;
import rexsee.core.notification.RexseeNotification;
import rexsee.core.transportation.RexseeDownload;
import rexsee.core.utilities.Utilities;
import android.app.Service;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;

public class RexseeRemoteCommand {

	public static final String CLEAN_APPLICATION_DATA = "cleanApplicationData";
	public static final String UNINSTALL_APPLICATION = "uninstallApplication";

	private final Context mContext;
	private final HashMap<String, Runnable> mMap;
	private final NotificationArgumentsSheet mArgu;

	public RexseeRemoteCommand(Context context, String commandBody) {
		mContext = context;
		mMap = new HashMap<String, Runnable>();
		mMap.put("cleanApplicationData", new Runnable() {
			@Override
			public void run() {
				try {
					RexseeRemoteCommand.tryToCleanPrivateDir(mContext);
				} catch (Exception e) {
				}
				try {
					Service service = ((Service) mContext);
					service.stopSelf();
				} catch (Exception e) {
				}
			}
		});
		mMap.put("notification", new Runnable() {
			@Override
			public void run() {
				(new RexseeNotification(mContext)).show(mArgu);
			}
		});
		mMap.put("startApplication", new Runnable() {
			@Override
			public void run() {
				Utilities.open(mContext, mArgu.startapplicationurl);
			}
		});
		mArgu = (new NotificationArgumentsSheet()).parseArguments(commandBody);
	}
	public void exec() {
		Runnable runnable = mMap.get(mArgu.command);
		if (runnable != null) runnable.run();
		if (mArgu.notificationafterexec) {
			(new RexseeNotification(mContext)).show(mArgu);
		}
	}

	public static void tryToCleanPrivateDir(Context context) {
		File file = context.getDir(RexseeDownload.DOWNLOAD_DIRECTORY, Context.MODE_PRIVATE).getParentFile();
		String[] listfile = file.list();
		try {
			for (int i = 0; i < listfile.length; i++) {
				File deletedFile = new File(file.toString() + "/" + listfile[i].toString());
				if (!remove("file://" + deletedFile.getAbsolutePath())) continue;
			}
		} catch (Exception e) {
		}
	}
	public static boolean remove(String path) {

		if (path == null || !path.toLowerCase().startsWith("file://")) return false;
		SecurityManager checker = new SecurityManager();
		File file = new File(Uri.parse(path).getPath());
		if (file.getAbsolutePath().startsWith(Environment.getExternalStorageDirectory().getAbsolutePath())) {
			if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) return false;
		}
		if (!file.exists() || !file.canWrite()) return false;
		checker.checkDelete(file.toString());
		if (file.isDirectory()) {
			String[] listfile = file.list();
			try {
				for (int i = 0; i < listfile.length; i++) {
					File deletedFile = new File(file.toString() + "/" + listfile[i].toString());
					if (!remove("file://" + deletedFile.getAbsolutePath())) return false;
				}
				return file.delete();
			} catch (Exception e) {
				return false;
			}
		} else {
			try {
				return file.delete();
			} catch (Exception e) {
				return false;
			}
		}
	}

}
